package com.pfzy.ui.activity.pick;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.pfzy.ui.R;
import com.pfzy.ui.activity.pick.adapter.NumericWheelAdapter;
import com.pfzy.ui.log.MyLog;
import com.pfzy.ui.utils.PickerViewAnimateUtil;
import com.pfzy.ui.view.wheelview.WheelView;
import com.pfzy.ui.view.wheelview.listener.OnItemSelectedListener;

import java.util.Calendar;

public class PickerDlg9 extends Dialog implements View.OnClickListener {
    private ViewGroup dialogView;// 附加Dialog 的 根View
    private ViewGroup contentContainer;
    private Dialog mDialog;
    private Animation outAnim;
    private Animation inAnim;
    private int animGravity = Gravity.BOTTOM;
    private int gravity = Gravity.CENTER;
    private int textSize = 18;
    private float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6
    private int textColorOut = 0xFFa8a8a8; // 分割线以外的文字颜色
    private int textColorCenter = 0xFF2a2a2a; // 分割线之间的文字颜色
    private boolean isCenterLabel = false;// 是否只显示中间的label,默认每个item都显示
    public boolean cyclic = false;//是否循环
    private WheelView wv_start_hours;
    private WheelView wv_start_minutes;
    private WheelView wv_end_hours;
    private WheelView wv_end_minutes;
    private int mStartHourStart = 0;
    private int mStartMinuteStart = 0;
    private int mEndHourStart = 0;
    private int mEndMinuteStart = 0;
    private OnTimeSelectListener mOnItemClickListener = null;

    public PickerDlg9(@NonNull Context context) {
        super(context);
        initWindow();
        initAnim();
        initView();
    }

    private void initWindow() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        // 如果是对话框模式
        dialogView = (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, null, false);
        // 设置界面的背景为透明
        dialogView.setBackgroundColor(Color.TRANSPARENT);
        // 这个是真正要加载选择器的父布局
        contentContainer = dialogView.findViewById(R.id.content_container);
        // 设置对话框 默认左右间距屏幕30
        params.leftMargin = 30;
        params.rightMargin = 30;
        contentContainer.setLayoutParams(params);

        // 创建对话框
        createDialog();
        //给背景设置点击事件,这样当点击内容以外的地方会关闭界面
        dialogView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    protected void initAnim() {
        inAnim = getInAnimation();
        outAnim = getOutAnimation();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.dlg_picker_8, contentContainer);
        // 确定和取消按钮
        Button btnSubmit = contentContainer.findViewById(R.id.dlg_picker_8_btn);
        ImageView btnCancel = contentContainer.findViewById(R.id.dlg_picker_8_close);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        wv_start_hours = contentContainer.findViewById(R.id.dlg_picker_8_start_hour);
        wv_start_minutes = contentContainer.findViewById(R.id.dlg_picker_8_start_minute);
        wv_end_hours = contentContainer.findViewById(R.id.dlg_picker_8_end_hour);
        wv_end_minutes = contentContainer.findViewById(R.id.dlg_picker_8_end_minute);

        wv_start_hours.setGravity(gravity);
        wv_start_minutes.setGravity(gravity);
        wv_end_hours.setGravity(gravity);
        wv_end_minutes.setGravity(gravity);

        wv_start_hours.setTextSize(textSize);
        wv_start_minutes.setTextSize(textSize);
        wv_end_hours.setTextSize(textSize);
        wv_end_minutes.setTextSize(textSize);

        wv_start_hours.setLabel(getContext().getString(R.string.hours));
        wv_start_minutes.setLabel(getContext().getString(R.string.minutes));
        wv_end_hours.setLabel(getContext().getString(R.string.hours));
        wv_end_minutes.setLabel(getContext().getString(R.string.minutes));

        wv_start_hours.setTextXOffset(0);
        wv_start_minutes.setTextXOffset(0);
        wv_end_hours.setTextXOffset(0);
        wv_end_minutes.setTextXOffset(0);

        wv_start_hours.setCyclic(cyclic);
        wv_start_minutes.setCyclic(cyclic);
        wv_end_hours.setCyclic(cyclic);
        wv_end_minutes.setCyclic(cyclic);

        wv_start_hours.setDividerColor(Color.YELLOW);
        wv_start_minutes.setDividerColor(Color.YELLOW);
        wv_end_hours.setDividerColor(Color.YELLOW);
        wv_end_minutes.setDividerColor(Color.YELLOW);

        wv_start_hours.setDividerType(WheelView.DividerType.FILL);
        wv_start_minutes.setDividerType(WheelView.DividerType.FILL);
        wv_end_hours.setDividerType(WheelView.DividerType.FILL);
        wv_end_minutes.setDividerType(WheelView.DividerType.FILL);

        wv_start_hours.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_start_minutes.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_end_hours.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_end_minutes.setLineSpacingMultiplier(lineSpacingMultiplier);

        wv_start_hours.setTextColorOut(textColorOut);
        wv_start_minutes.setTextColorOut(textColorOut);
        wv_end_hours.setTextColorOut(textColorOut);
        wv_end_minutes.setTextColorOut(textColorOut);

        wv_start_hours.setTextColorCenter(textColorCenter);
        wv_start_minutes.setTextColorCenter(textColorCenter);
        wv_end_hours.setTextColorCenter(textColorCenter);
        wv_end_minutes.setTextColorCenter(textColorCenter);

        wv_start_hours.isCenterLabel(isCenterLabel);
        wv_start_minutes.isCenterLabel(isCenterLabel);
        wv_end_hours.isCenterLabel(isCenterLabel);
        wv_end_minutes.isCenterLabel(isCenterLabel);
        
        int hours, minute;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        hours = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        wv_start_hours.setAdapter(new NumericWheelAdapter(0, 23));
        wv_start_hours.setCurrentItem(hours);

        wv_start_minutes.setAdapter(new NumericWheelAdapter(0, 59));
        wv_start_minutes.setCurrentItem(minute);

        wv_end_hours.setAdapter(new NumericWheelAdapter(0, 23));
        wv_end_hours.setCurrentItem(hours);

        wv_end_minutes.setAdapter(new NumericWheelAdapter(0, 59));
        wv_end_minutes.setCurrentItem(minute);

        wv_start_hours.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {// 选中项目回调
                int hour = getStartHours();
                int minutes = getStartMinutes();
                _logd("开始时间选择 " + hour + "时");
                setEndHours(hour);
                if (getEndHours() > hour) {
                    setEndMinutes(0);
                } else {
                    setEndMinutes(minutes);
                }
            }
        });
        wv_start_minutes.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {// 选中项目回调
                int hour = getStartHours();
                int minutes = getStartMinutes();
                _logd("开始时间选择 " + hour + "时" + minutes + "分");
                if (getEndHours() > hour) {
                    setEndMinutes(0);
                } else {
                    setEndMinutes(minutes);
                }
            }
        });
        wv_end_hours.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {// 选中项目回调
                _logd("结束时间选择 " + getEndHours() + "时" + getEndMinutes() + "分");
                if (getEndHours() > getStartHours()) {
                    setEndMinutes(0);
                } else {
                    setEndMinutes(getStartMinutes());
                }
            }
        });
        wv_end_minutes.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {// 选中项目回调
            }
        });
    }

    public void createDialog() {
        if (dialogView != null) {
            mDialog = new Dialog(getContext(), R.style.custom_dialog2);
            mDialog.setCancelable(false);
            mDialog.setContentView(dialogView);
            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(R.style.pickerview_dialogAnim);
                dialogWindow.setGravity(Gravity.CENTER);
            }
        }
    }

    public Animation getInAnimation() {
        int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, true);
        return AnimationUtils.loadAnimation(getContext(), res);
    }

    public Animation getOutAnimation() {
        int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, false);
        return AnimationUtils.loadAnimation(getContext(), res);
    }

    public int getStartHours() {
        return wv_start_hours.getCurrentItem() + mStartHourStart;
    }

    public void setStartHours(int hours) {
        wv_start_hours.setAdapter(new NumericWheelAdapter(hours, 23));
        mStartHourStart = hours;
    }

    public int getEndHours() {
        return wv_end_hours.getCurrentItem() + mEndHourStart;
    }

    public void setEndHours(int hours) {
        wv_end_hours.setAdapter(new NumericWheelAdapter(hours, 23));
        mEndHourStart = hours;
    }

    public int getStartMinutes() {
        return wv_start_minutes.getCurrentItem() + mStartMinuteStart;
    }

    public void setStartMinutes(int minutes) {
        wv_start_minutes.setAdapter(new NumericWheelAdapter(minutes, 59));
        mStartMinuteStart = minutes;
    }

    public int getEndMinutes() {
        return wv_end_minutes.getCurrentItem() + mEndMinuteStart;
    }

    public void setEndMinutes(int minutes) {
        wv_end_minutes.setAdapter(new NumericWheelAdapter(minutes, 59));
        mEndMinuteStart = minutes;
    }

    public void showDialog() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dlg_picker_8_close:
                dismissDialog();
                break;
            case R.id.dlg_picker_8_btn:
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onTimeSelect(getStartHours(), getStartMinutes(), getEndHours(), getEndMinutes());
                }
                dismissDialog();
                break;
            default:
                break;
        }
    }

    public void setOnItemClickListener(OnTimeSelectListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnTimeSelectListener {
        void onTimeSelect(int startHour, int startMinutes, int endHour, int endMinutes);
    }

    private void _logd(String msg) {
        MyLog.logd("时钟选择器9 : " + msg);
    }
}
